import React from 'react';
import { Tree, Button, Spin, Message } from '@arco-design/web-react';
import { fetchUserList } from '../../../../../api/common'
function DeptTree({state, setState}){
    const data = [
        {
          id: '1',
          children: [
            {
              id: '10416',
              parentCode: '370200000000',
              children: [
                {
                  id: '10541',
                  parentCode: '370202000000',
                  deptName: '文一路',
                  deptCode: '370202140000',
                },
                {
                  id: '10401',
                  parentCode: '370202000000',
                  deptName: '文二路',
                  deptCode: '370202150000',
                },
                {
                  id: '10398',
                  parentCode: '370202000000',
                  children: [
                    {
                      id: '10628',
                      parentCode: '370202230000',
                      deptName: '文三路',
                      deptCode: '370202230001',
                    },
                    {
                      id: '10629',
                      parentCode: '370202230000',
                      deptName: '文晖路',
                      deptCode: '370202230002',
                    },
                  ],
                  deptName: '古翠路',
                  deptCode: '370202230000',
                },
                {
                  id: '10537',
                  parentCode: '370202000000',
                  deptName: '丰潭路',
                  deptCode: '370202240000',
                },
              ],
              deptName: '下城区',
              deptCode: '370202000000',
            },
          ],
          deptName: '杭州市',
          deptCode: '370200000000',
        },
      ]

    function handleSelectNode(key,extra){
      setState(state=>{
        return{
          ...state,
          userListResult: { ...state.userListResult, loading: true },
          searchKey: {
            ...state.searchKey,
            deptCode: extra.node.props.deptCode,
          },
          moduletype: extra.node.props.deptName
        }
      })
      fetchUserList({
        moduletype: state.moduletype,
        searchKey: state.searchKey
      },
      (res)=>{
          setState((state)=>{
            return{
                ...state,
                userListResult: {
                    ...state.userListResult,
                    list: res.data.list,
                    loading: false  
                },
                searchKey: {
                  ...state.searchKey,
                  pageSize: res.data.pageSize,
                  pageNo: res.data.pageNo,
                  total: res.data.totalCount
              }
            }
        })
      },
      (error)=>{
        Message.error(error)
    })
    }
    return(
        <Spin loading={state.userDeptResult.loading} style={{paddingLeft: '10px', paddingTop: '14px'}}>
          <Tree defaultSelectedKeys={[state.userDeptResult.default] || []} 
              fieldNames={
                  {
                      key: 'id',
                      title: 'deptName',
                      children: 'children'
                  }
              }
              treeData={state.userDeptResult.list} 
              showLine={true}
              onSelect={handleSelectNode}
          ></Tree>
        </Spin>
    )
}

export default DeptTree